
/* Generated by Interface Builder */

#import <streams/streams.h>
#import "PreferencesController.h"
#import "CookieFile.h"
#import <sys/types.h>
#include <sys/stat.h>
#import <sys/dir.h>
#import <sys/param.h>
#import <defaults.h>
#import "CookieFileListView.h"
#import "CookieController.h"


extern NXDefaultsVector mydefaults;

@implementation PreferencesController

- setSeconds :(int)secs
{
	char string[32],temp[32];

	seconds = secs;

	string[0] = 0;
	switch((secs/60))
	{
		case 0	:	break;

		case 1	:	strcpy(string,"1 min ");
					break;

		default	:	sprintf(string,"%d mins ",secs/60);
					break;
	}

	switch((secs%60))
	{
		case 0	:	break;

		case 1	:	strcat(string,"1 sec");
					break;

		default	:	sprintf(temp,"%d secs",secs%60);
					strcat(string,temp);
					break;
	}

	if(secs == 0)
		strcpy(string,"no secs");

	[timeDisplay setStringValue:string];
	[timeDisplay display];

	return self;
}

- revert:sender
{
	[NXApp stopModal];
	[prefPanel orderOut:self];

	return self;
}

- switchTimer:sender
{
	flags ^= PREF_TIMER;

	return self;
}

- ok:sender
{
	NXStream *stream;
	char mypath[MAXPATHLEN];
	int count;
	NXRect myRect;

	sprintf(mypath,"%s/.Cookie_Defaults",getenv("HOME"));
	stream = NXOpenMemory(NULL,0,NX_READWRITE);
	if(!stream)
		return 0;

	for(count = 0; count < [((List *)cookieList) count]; count++)
	{
		CookieFile *cookieFile;
		int length;
		char myflags = 0;

		cookieFile = [cookieList objectAt:count];
		if([[[((CookieFileListView *)fileList) matrix] cellAt:count:0]  state])
		 	[cookieFile setSelected:TRUE];
		else
			[cookieFile setSelected:FALSE];

		length = strlen([cookieFile filename])+1;
		NXWrite(stream,(char *)&length,sizeof(int));
		NXWrite(stream,[cookieFile filename],length);

		if([cookieFile selected])
			myflags = TRUE;
		NXWrite(stream,(char *)&myflags,1);
	}

	if(NXSaveToFile(stream,mypath))
		{
		NXCloseMemory(stream,NX_FREEBUFFER);
		return 0;
		}

	NXCloseMemory(stream,NX_FREEBUFFER);

	if(flags&PREF_DEAD)
		NXWriteDefault([NXApp appName],"Status","DEAD");
	else
		NXWriteDefault([NXApp appName],"Status","ALIVE");

	if(flags&PREF_TIMER)
		NXWriteDefault([NXApp appName],"Timer","YES");
	else
		NXWriteDefault([NXApp appName],"Timer","NO");

	sprintf(mypath,"%d",seconds);
	NXWriteDefault([NXApp appName],"Seconds",mypath);

	myRect = [cookieController getWindowRect];
	sprintf(mypath,"%.0f %.0f %.0f %.0f",myRect.origin.x,myRect.origin.y,myRect.size.width,
		myRect.size.height);
	NXWriteDefault([NXApp appName],"MainFrame",mypath);

	NXWriteDefault([NXApp appName],"NXFont",[cookieController fontname]);

	myRect.origin.x = [((CookieController *)cookieController) fontsize];
	sprintf(mypath,"%.0f",myRect.origin.x);
	NXWriteDefault([NXApp appName],"NXFontSize",mypath);

	[NXApp stopModal];
	[prefPanel orderOut:self];
	[cookieController newPrefs];

	return self;
}

- changeTimer:sender
{
	float newValue;

	newValue = [sender floatValue];

	[self setSeconds:((int)newValue)];

	return self;
}

- startPreferences:sender
{
	[fileList syncView:cookieList];
	if(flags&PREF_TIMER)
		[timerButton setState:FALSE];
	else
		[timerButton setState:TRUE];
	if(flags&PREF_DEAD)
		[statusButton setState:TRUE];
	else
		[statusButton setState:FALSE];

	[timerSlider setFloatValue:(float)seconds];

	[timerButton display];
	[statusButton display];
	[timerSlider display];
	[self setSeconds:seconds];

	[prefPanel makeKeyAndOrderFront:self];

	[NXApp runModalFor:prefPanel];

	return self;
}

- changeStatus:sender
{
	flags ^= PREF_DEAD;

	return self;
}

- getPreferences
{
	NXStream *stream;
	int entries = 0,dircount;
	struct direct **namelist;
	char mypath[MAXPATHLEN];

	[cookieList init];

	sprintf(mypath,"%s/Library/Cookies",getenv("HOME"));

	for(dircount = 0; dircount < 2; dircount++)
	{
		// Try resource in users home directory, then system wide one.
		switch(dircount)
		{
			case 0	:
				entries = scandir(mypath, &namelist,NULL,NULL);
				break;

			case 1	:
				entries = scandir("/LocalLibrary/Cookies",&namelist,NULL,NULL);
				break;
		}

		if(entries > 0)
		{
			int count;

			for(count = 0; count < entries; count++)
				{
					CookieFile *cookieFile;

					// Skip over this filename if it starts with a dot
					if(namelist[count]->d_name[0] == '.')
						continue;

					// Must be a cookie file them, make a cookie file for it
					cookieFile = [CookieFile alloc];
					if([cookieFile initFromName:namelist[count]->d_name fromList:cookieList])
					{
						struct stat my_stat;
						char filepath[MAXPATHLEN];

						switch(dircount)
						{
							case 0	:
								sprintf(filepath,"%s/%s",mypath,namelist[count]->d_name);
								break;

							case 1	:
								sprintf(filepath,"%s/%s","/LocalLibrary/Cookies",
									namelist[count]->d_name);
								break;
						}
						stat(filepath,&my_stat);

						[cookieFile setFilelength:my_stat.st_size];
						[cookieList addObject:cookieFile];
					}
					else
						[cookieFile free];
				}
			// Free all the entries and the list itself
			for(count = 0; count < entries; count++)
				free((char *)namelist[count]);
			free((char *)namelist);
		}
	}

	sprintf(mypath,"%s/.Cookie_Defaults",getenv("HOME"));
	stream = NXMapFile(mypath, NX_READONLY);
	if(stream)
	{
		int count;
		char name[MAXPATHLEN];

		while(NXRead(stream,(char *)&count,sizeof(int)) == sizeof(int))
		{
			unsigned char myflags;
			CookieFile *cookieFile;

			NXRead(stream,name,count);
			NXRead(stream,(char *)&myflags,1);

			cookieFile = [CookieFile findName :name fromList:cookieList];
			if(cookieFile)
			{
				if(myflags)
					[cookieFile setSelected:TRUE];
				else
					[cookieFile setSelected:FALSE];
			}
		}

		NXCloseMemory(stream, NX_FREEBUFFER);
	}

	if(!strcasecmp(NXGetDefaultValue([NXApp appName],"Status"),"ALIVE"))
		flags = 0;
	else
		flags = PREF_DEAD;

	if(!strcasecmp(NXGetDefaultValue([NXApp appName],"Timer"),"YES"))
		flags |= PREF_TIMER;

	sscanf(NXGetDefaultValue([NXApp appName],"Seconds"),"%d",&seconds);
	[cookieController newPrefs];

	return self;
}

- (int)seconds
{
	return seconds;
}

- (char)flags
{
	return flags;
}

- (const char *)fontname
{
	return NXGetDefaultValue([NXApp appName],"NXFont");
}

- (int)fontsize
{
	return atoi(NXGetDefaultValue([NXApp appName],"NXFontSize"));
}

- (NXRect)windowframe
{
	NXRect myRect;

	sscanf(NXGetDefaultValue([NXApp appName],"MainFrame"),"%f %f %f %f",
		&myRect.origin.x,&myRect.origin.y,&myRect.size.width,&myRect.size.height);

	return myRect;
}

@end
